package com.tjhk.java.tech.thread;

import java.time.LocalDateTime;
import java.util.Locale;
import java.util.stream.Stream;

public class SellTicketDemo implements Runnable{
    private int ticket;

    public SellTicketDemo() {
        this.ticket = 1000;
    }

    @Override
    public void run() {
        while (ticket > 0){
            synchronized (this){
                if (ticket > 0) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + "-还剩：" + ticket-- +"@"+ LocalDateTime.now());
                }
            }
            Thread.yield();
        }
    }

    public static void main(String[] args) {
        SellTicketDemo demo = new SellTicketDemo();
        Thread t1 = new Thread(demo,"Thread1");
        Thread t2 = new Thread(demo,"Thread2");
        Thread t3 = new Thread(demo,"Thread3");
        Thread t4 = new Thread(demo,"Thread4");
        t1.setPriority(Thread.MIN_PRIORITY);
        t4.setPriority(Thread.MAX_PRIORITY);
        t3.setPriority(Thread.NORM_PRIORITY);
        t1.start();
        t4.start();
        t3.start();
        t2.start();
    }
}
